package com.qfedu.service.impl;

import com.qfedu.dao.ICartDao;
import com.qfedu.dao.IProductDao;
import com.qfedu.dao.impl.CartDaoImpl;
import com.qfedu.dao.impl.ProductDaoImpl;
import com.qfedu.entity.Cart;
import com.qfedu.entity.Product;
import com.qfedu.entity.Result;
import com.qfedu.service.ICartService;

import java.util.List;

public class CartServiceImpl implements ICartService {
    private ICartDao cartDao = new CartDaoImpl();
    private IProductDao productDao = new ProductDaoImpl();
    @Override
    public Result insertCart(int uid, int pid) {

        Product product = productDao.getProduct(pid);
        String money = product.getPprice();
        Cart cart = cartDao.checkCart(uid, pid);
        if (cart==null){
            int i = cartDao.insertCart(uid, pid,Double.parseDouble(money));
            if (i!=0){
                return new Result("购物车加入商品成功",true);
            }else {
                return new Result("购物车加入商品失败",false);
            }
        }else {
            int i = cartDao.addCart(uid, pid);
            if (i!=0){
                return new Result("商品添加数量成功",true);
            }else {
                return new Result("商品添加数量失败",false);
            }
        }
    }

    @Override
    public Result cartList(int uid) {
        List<Cart> carts = cartDao.listCart(uid);
        if (carts.size()==0){
            return new Result("你的购物车空空如也",false,carts);
        }else {
            return new Result("",true,carts);
        }

    }

    @Override
    public Result addCartCount(int uid, int pid) {
        Product product = productDao.getProduct(pid);
        String money = product.getPprice();
        int i = cartDao.insertCart(uid, pid,Double.parseDouble(money));
        if (i!=0){
            return new Result("商品数量添加成功",true);
        }else {
            return new Result("商品数量添加失败",false);
        }
    }

    @Override
    public Result subtractCart(int uid, int pid) {
        int i = cartDao.subtractCart(uid, pid);
        if (i!=0){
            return new Result("商品数量减少成功",true);
        }else {
            return new Result("商品数量减少失败",false);
        }
    }

    @Override
    public Result changeCartCount(int uid, int pid, int count) {
        int i = cartDao.changeCartCount(uid, pid,count);
        if (i!=0){
            return new Result("商品数量修改成功",true);
        }else {
            return new Result("商品数量修改失败",false);
        }
    }

    @Override
    public Result deleteCart(int uid, int pid) {
        int i = cartDao.deleteCart(uid,pid);
        if (i!=0){
            return new Result("商品删除成功",true);
        }else {
            return new Result("商品删除失败",false);
        }
    }

    @Override
    public Result clearCart(int uid) {
        int i = cartDao.clearCart(uid);
        if (i!=0){
            return new Result("购物车清空成功",true);
        }else {
            return new Result("购物车清空失败",false);
        }
    }

    @Override
    public List<Cart> getCartList(int uid) {
        List<Cart> cartList = cartDao.listCart(uid);
        //List<Product> products = new ArrayList<>();
        for (Cart cart : cartList) {
           // Product product = new Product();
            Product product = productDao.getProduct(cart.getPid());
            cart.setPname(product.getPname());
            cart.setPprice(product.getPprice());
        }
        return cartList;
    }
    
}
